<?php
namespace App\Http\Controllers\Index;

use App\Http\Responses\DemoResp;
use App\Logic\Code;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Order;

class WalletController extends Controller
{

    /*
     * 1.用户钱包
     * 2.收益记录
     * 3.提现
     * */
    /**
     * @OA\Post(
     *     path="/wallet_bind",
     *     tags={"钱包"},
     *     summary="绑定钱包",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *     @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"address","code","token"},
     *                 @OA\Property(property="address", type="string", description="钱包地址"),
     *                 @OA\Property(property="token", type="string", description="币种类型"),
     *                 @OA\Property(property="code", type="number", description="验证码"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function bind(Request $request)
    {
        $post_data = $request->all();
        $validateRule = [
            'address' => 'required',
            'token' => 'required',
        ];
        $errorMsg = [
            'address.required' => '请传入钱包地址',
            'token.required' => '请传入币种信息',
        ];

        $validator = Validator::make($post_data, $validateRule, $errorMsg);
        if ($validator->fails()) {
            $errors = $validator->errors()->all();
            return e_msg(300001, $errors[0]);
        }


        $is_exists = DB::table('miner_wallet')
            ->where('user_name', $this->user['username'])
            ->where('token', $post_data['token'])
            ->value('mwid');
        $arr = [
            'user_name' => $this->user['username'],
            'token' => $post_data['token'],
            'address' => $post_data['address'],
        ];

        if ($is_exists) {
            $rel = DB::table('miner_wallet')->where('mwid', $is_exists)->update($arr);
        } else {
            $rel = DB::table('miner_wallet')->insert($arr);
        }

        if ($rel) {
            return s_msg('绑定成功');
        } else {
            return e_msg(300006, '地址已绑定请勿重复输入');
        }

    }

    /**
     * @OA\Post(
     *     path="/wallet_list",
     *     tags={"钱包"},
     *     summary="钱包列表",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *     @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"token"},
     *                 @OA\Property(property="token", type="string", description="类型"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function wallet_list(Request $request)
    {
        $where = [];
        if($request->has('limit')){
            $limit = $request->input('limit');
        }else{
            $limit = 15;
        }
        if($request->has('token')){
            $token = $request->input('token');
            $where[] = ['token','=',$token];
        }

        $wallet_list = DB::table('miner_wallet')
            ->where('user_name',$this->user['username'])
            ->where($where)
            ->paginate(15);
        return s_msg('', $wallet_list);
    }

    /**
     * @OA\Post(
     *     path="/wallet_bank",
     *     tags={"钱包"},
     *     summary="绑定银行卡",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *     @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 @OA\Property(property="bank_name", type="string", description="银行名称"),
     *                 @OA\Property(property="bank_account", type="string", description="账户名"),
     *                 @OA\Property(property="bank_legal", type="number", description="法人"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function bank(Request $request)
    {
        $post_data = $request->all();
        $validateRule = [
            'bank_name' => 'required',
            'bank_account' => 'required',
            'bank_legal' => 'required',
        ];
        $errorMsg = [
            'bank_name.required' => '请传入银行名称',
            'bank_account.required' => '请传入银行账号',
            'bank_legal.required' => '请传入开户人',
        ];

        $validator = Validator::make($post_data, $validateRule, $errorMsg);
        if ($validator->fails()) {
            $errors = $validator->errors()->all();
            return e_msg(300001, $errors[0]);
        }

        $arr = [
            'bank_name' => $post_data['bank_name'],
            'bank_account' => $post_data['bank_account'],
            'bank_legal' => $post_data['bank_legal'],
        ];

       DB::table('user')->where('id',$this->user['id'])->update($arr);
       return s_msg('修改成功');

    }



}
